package com.wing.bill.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.dao.RepairAndScarpInReportDao;
import com.wing.cabinet.dao.StorageInventoryInoutDao;
import com.wing.cabinet.model.response.StorageInventoryInoutVO;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.LocalDateTimeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RepairAndScarpInReportServiceImpl implements RepairAndScarpInReportService {
    @Autowired
    private RepairAndScarpInReportDao repairAndScarpInReportDao;

    @Autowired
    private StorageInventoryInoutDao storageInventoryInoutDao;

    @Override
    public JsonResult list(Integer businessType,String param,  String enterpriseCode) {
        String title = "";
        if (businessType == 2){
            title = "报修";
        }else if (businessType == 3){
            title = "报废";
        }
        HashMap hm = new HashMap();
        hm.put("title","物料"+title+"统计(按天)");
        hm.put("data", repairAndScarpInReportDao.list(businessType,param,enterpriseCode));
        return JsonResult.success(hm);
    }

    @Override
    public JsonResult topCell(Integer businessType,String startTime, String endTime, Integer click, String mainCabCode,String enterpriseCode) {
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
            String title = "";
            if (businessType == 2){
                title = "报修";
            }else if (businessType == 3){
                title = "报废";
            }
            if (click == 0) {
                HashMap map1 = repairAndScarpInReportDao.queryInNum(businessType,start, end, mainCabCode,enterpriseCode);
                map1.put("title", title+"存入数量");
                map1.put("sign", "1");

                HashMap map2 = repairAndScarpInReportDao.queryInCreatorNum(businessType,start, end, mainCabCode,enterpriseCode);
                map2.put("title", title+"存入人数");
                map2.put("sign", "2");

                HashMap map3 = repairAndScarpInReportDao.queryInDeviceNum(businessType,start, end, mainCabCode,enterpriseCode);
                map3.put("title", title+"存入机台数");
                map3.put("sign", "3");

                HashMap map4 = repairAndScarpInReportDao.queryNumByStatus(businessType,0,start, end, mainCabCode,enterpriseCode);
                map4.put("title", title+"数量(待盘验)");
                map4.put("sign", "4");

                HashMap map5 = repairAndScarpInReportDao.queryNumByStatus(businessType,1,start, end, mainCabCode,enterpriseCode);
                map5.put("title", title+"数量(已盘验)");
                map5.put("sign", "5");

                HashMap map6 = repairAndScarpInReportDao.queryCheckNum(businessType,start, end, mainCabCode,enterpriseCode);
                map6.put("title", title+"盘验异常数量");
                map6.put("sign", "6");


                List list = new ArrayList();
                list.add(map1);
                list.add(map2);
                list.add(map3);
                list.add(map4);
                list.add(map5);
                list.add(map6);
                return JsonResult.success("查询成功", list);
            } else if (click == 1) {
                return JsonResult.success("查询成功",repairAndScarpInReportDao.queryInNumClick(businessType,start, end, mainCabCode,enterpriseCode));
            } else if (click == 2) {
                return JsonResult.success("查询成功",repairAndScarpInReportDao.queryInCreatorNumClick(businessType,start, end, mainCabCode,enterpriseCode));
            } else if (click == 3) {
                return JsonResult.success("查询成功",repairAndScarpInReportDao.queryInDeviceNumClick(businessType,start, end, mainCabCode,enterpriseCode));
            } else if (click == 4) {
                return JsonResult.success("查询成功",repairAndScarpInReportDao.queryNumByStatusClick(businessType,0,start, end, mainCabCode,enterpriseCode));
            } else if (click == 5) {
                return JsonResult.success("查询成功",repairAndScarpInReportDao.queryNumByStatusClick(businessType,1,start, end, mainCabCode,enterpriseCode));
            } else if (click == 6) {
                return JsonResult.success("查询成功",repairAndScarpInReportDao.queryCheckNumClick(businessType,start, end, mainCabCode,enterpriseCode));
            }
        }
        return null;
    }

    @Override
    public JsonResult queryStatisticsByParam(Integer businessType, String selectParam, String param, String startTime, String endTime, String mainCabCode, String enterpriseCode, String type, String tab, Map<String, Object> screenParam) {
        String title = "";
        String title2 = "";
        if (businessType == 2){
            title = "物料报修";
            title2 = "报修";
        }else if (businessType == 3){
            title = "物料报废";
            title2 = "报废";
        }

        HashMap hm = new HashMap();
        if ("tab0".equals(tab)) {
            if ("sku".equals(type)) {
                hm.put("title", title+"排名(按物料)");
            } else if ("cab".equals(type)) {
                hm.put("title", title+"排名(按柜子)");
            } else if ("org".equals(type)) {
                hm.put("title", title+"排名(按组织)");
            } else if ("use".equals(type)) {
                hm.put("title", title+"排名(按使用情况)");
            } else if ("reason".equals(type)) {
                hm.put("title", title2+"原因统计");
            }
        } else if ("tab1".equals(tab) && "spu".equals(selectParam)) {
            hm.put("title", title+"排名(按系列SPU)");
        } else if ("tab1".equals(tab) && "sku".equals(selectParam)) {
            hm.put("title", title+"排名(按SKU)");
        } else if ("tab1".equals(tab) && "type".equals(selectParam)) {
            hm.put("title", title+"排名(按类型)");
        } else if ("tab1".equals(tab) && "category".equals(selectParam)) {
            hm.put("title", title+"排名(按分类)");
        } else if ("tab1".equals(tab) && "brand".equals(selectParam)) {
            hm.put("title", title+"排名(按品牌)");
        } else if ("tab1".equals(tab) && "main".equals(selectParam)) {
            hm.put("title", title+"排名(按主柜)");
        } else if ("tab1".equals(tab) && "desk".equals(selectParam)) {
            hm.put("title", title+"排名(按副柜)");
        }  else if ("tab1".equals(tab) && "person".equals(selectParam)) {
            hm.put("title", title+"排名(按"+title2+"人)");
        } else if ("tab1".equals(tab) && "department".equals(selectParam)) {
            hm.put("title", title+"排名(按部门)");
        } else if ("tab1".equals(tab) && "costCenter".equals(selectParam)) {
            hm.put("title", title+"排名(按成本中心)");
        } else if ("tab1".equals(tab) && "workOrder".equals(selectParam)) {
            hm.put("title", title+"排名(按工单)");
        } else if ("tab1".equals(tab) && "workShop".equals(selectParam)) {
            hm.put("title", title+"排名(按车间)");
        } else if ("tab1".equals(tab) && "device".equals(selectParam)) {
            hm.put("title", title+"排名(按设备)");
        }


        List<HashMap> list = new ArrayList<>();
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)) {
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");

            if ("reason".equals(type)) {
                list.addAll(repairAndScarpInReportDao.queryReason(businessType, screenParam, start, end, mainCabCode, enterpriseCode));
            } else {
                list.addAll(repairAndScarpInReportDao.queryStatisticsByParam(businessType, selectParam, param, start, end, mainCabCode, enterpriseCode));
            }

            if ("department".equals(selectParam)||"costCenter".equals(selectParam)){
                hm.put("totalSum",repairAndScarpInReportDao.getTotal(businessType,start,end,param,mainCabCode,enterpriseCode));
            }

        }

        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

    @Override
    public Page data(Integer businessType, String startTime, String endTime, String mainCabCode, String enterpriseCode, Page page) {
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {

            Map<String, Object> params = new HashMap<>();
            params.put("businessType",businessType);
            params.put("startCreateTime",startTime);
            params.put("endCreateTime",endTime);
            params.put("mainCabCode",mainCabCode);
            params.put("enterpriseCode",enterpriseCode);

            List<StorageInventoryInoutVO> list = storageInventoryInoutDao.list(page,params);

            page.setRecords(list);
            return page;
        }
        return null;
    }
}
